{% extends "admin/layout.html" %}

{% block content %}
<div class="card">
    <div class="card-header">
        <h4>分配任务: {{ task.title }}</h4>
    </div>
    <div class="card-body">
        <form method="POST" action="{{ url_for('jira.assign_task', task_id=task.id) }}">
            <input name="csrf_token" type="hidden" value="{{ csrf_token() }}">
            <div class="mb-3">
                <h6>任务详情</h6>
                <div class="card mb-3">
                    <div class="card-body">
                        <h5>{{ task.title }}</h5>
                        <p class="text-muted">{{ task.description or '无描述' }}</p>
                        <p>所属阶段: {{ task.stage.name }}</p>
                        <p>当前状态: {{ task.status }}</p>
                    </div>
                </div>

                <div class="row">
                    <div class="col-md-6">
                        <label class="form-label">选择成员 *</label>
                        <select name="member_id" class="form-select member-select" required style="width: 100%">
                            <option value="">-- 请选择成员 --</option>
                            {% for member in project.members %}
                            <option value="{{ member.member_id }}"
                                    data-avatar="{{ url_for('static', filename='nopic.jpg') }}"
                                    {% if task.assignee_id == member.member_id %}selected{% endif %}>
                                {{ member.member.name }} ({{ member.member.email }})
                            </option>
                            {% endfor %}
                        </select>
                        <small class="text-muted">请为任务选择合适的负责人</small>
                    </div>
                    <div class="col-md-6">
                        <label class="form-label">任务优先级</label>
                        <select name="priority" class="form-select">
                            <option value="low" {% if task.priority == 'low' %}selected{% endif %}>低</option>
                            <option value="medium" {% if task.priority == 'medium' %}selected{% endif %}>中</option>
                            <option value="high" {% if task.priority == 'high' %}selected{% endif %}>高</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="mb-3">
                <label class="form-label">任务说明</label>
                <textarea name="instructions" class="form-control" rows="3">{{ task.instructions or '' }}</textarea>
            </div>
            <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                <button type="submit" class="btn btn-primary">确认分配</button>
                <a href="{{ url_for('jira.project_detail', project_id=project.id) }}"
                   class="btn btn-secondary">取消</a>
            </div>
        </form>
    </div>
</div>
{% endblock %}

{% block scripts %}
<script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 初始化成员选择器
    $('.member-select').select2({
        placeholder: "搜索并选择成员...",
        templateResult: formatMember,
        templateSelection: formatMember
    });

    function formatMember(member) {
        if (!member.id) return member.text;

        var $member = $(
            '<div class="d-flex align-items-center">' +
            '<img src="' + $(member.element).data('avatar') + '" class="rounded-circle me-2" width="24" height="24">' +
            '<span>' + member.text + '</span>' +
            '</div>'
        );
        return $member;
    }
});
</script>
{% endblock %}